const ora = require('ora');
const util = require("../util/helper");
const cliui = require('cliui');
const chalk = require('chalk');

module.exports = function (hooker) {
	let adaSpinner = null;
	let initerSpinner = null;
	let workerSpinner = null;
	let mapSpinner = null;
	let mapTime = 0;
	let installSpinner = null;
	let cloneSpinner = null;
	hooker.hook("before-pack", () => { })
		.hook("start-install", ({ data }) => {
			let { name } = data;
			installSpinner && installSpinner.stop();
			installSpinner = ora({ color: "yellow", text: "INSTALL " + name }).start();
		})
		.hook("after-install", ({ data }) => {
			let { name } = data;
			installSpinner && installSpinner.stop();
			console.log(chalk.green(`INSTALL`), chalk.green(`|`), name);
		})
		.hook("start-install-all", ({ data }) => {
			let { name } = data;
			installSpinner = ora({ color: "yellow", text: "INSTALL " + name }).start();
		})
		.hook("after-install-all", ({ data }) => {
			let { name } = data;
			installSpinner && installSpinner.stop();
			console.log(chalk.green(`INSTALL`), chalk.green(`|`), name);
		})
		.hook("install-all-error", ({ data }) => {
			let { name } = data;
			installSpinner && installSpinner.stop();
			console.log(chalk.red(`INSTALL`), chalk.green(`|`), name);
		})
		.hook("before-map", () => {
			mapTime = new Date().getTime();
			mapSpinner = ora({ color: "yellow", text: "MAP SOURCE" }).start();
		})
		.hook("before-make", () => { })
		.hook("after-make", () => { })
		.hook("after-map", ({ data }) => {
			let { name } = data;
			mapSpinner && mapSpinner.stop();
			let header = cliui();
			header.div(
				{ text: chalk.white('APP'), width: 70 },
				{ text: chalk.white('TIME'), width: 15 }
			);
			let ui = cliui();
			ui.div(
				{ text: chalk.green(name), width: 70 },
				{ text: chalk.green(`${(new Date().getTime() - mapTime)}ms`), width: 15 }
			);
			console.log(chalk.gray(('+ ' + util.formatDate('mm:ss') + ' +').padEnd(98, '-') + '+'));
			console.log(chalk.cyan(`MAPPING`), chalk.yellow(`|`), header.toString());
			console.log('       ', chalk.green(`|`), ui.toString());
		})
		.hook('map-error', e => {
			mapSpinner && mapSpinner.stop();
			console.log(e);
			console.log(chalk.red(`${e.data}`));
			// process.exit(0);
		})
		.hook("before-output", () => { })
		.hook("before-pack-ada-core", () => {
			adaSpinner = ora({ color: "yellow", text: "BUNDLE ADA CORE" }).start();
		})
		.hook("after-pack-ada-core", ({ data }) => {
			let { bundler } = data;
			adaSpinner && adaSpinner.stop();
			let header = cliui();
			header.div(
				{ text: chalk.cyan('PACK TIME'), width: 70 },
				{ text: chalk.cyan('SIZE'), width: 15 },
				{ text: chalk.cyan('GZIP'), width: 15 }
			);
			let ui = cliui();
			ui.div(
				{ text: chalk.green(`${bundler.time}ms`), width: 70 },
				{ text: chalk.green(bundler.getFileSize()), width: 15 },
				{ text: chalk.green(bundler.getGzipSize()), width: 15 }
			);
			console.log(chalk.cyan(`ADACORE`), chalk.cyan(`|`), header.toString());
			console.log('       ', chalk.green(`|`), ui.toString());
		})
		.hook("before-hooker", () => {
			initerSpinner = ora({ color: "yellow", text: "BUNDLE INITER CODE" }).start();
		})
		.hook("after-hooker", ({ data }) => {
			let { bundler } = data;
			initerSpinner && initerSpinner.stop();
			let header = cliui();
			header.div(
				{ text: chalk.cyan('TIME'), width: 70 },
				{ text: chalk.cyan('SIZE'), width: 15 },
				{ text: chalk.cyan('GZIP'), width: 15 }
			);
			let ui = cliui();
			ui.div(
				{ text: chalk.green(`${bundler.time}ms`), width: 70 },
				{ text: chalk.green(bundler.getFileSize()), width: 15 },
				{ text: chalk.green(bundler.getGzipSize()), width: 15 }
			);
			console.log(chalk.cyan(`HOOKERC`), chalk.cyan(`|`), header.toString());
			console.log('       ', chalk.green(`|`), ui.toString());
		})
		.hook("before-worker", () => {
			workerSpinner = ora({ color: "yellow", text: "BUNDLE WORKER" }).start();
		})
		.hook("after-worker", (bundler) => {
			workerSpinner && workerSpinner.stop();
			let header = cliui();
			header.div(
				{ text: chalk.cyan('OUTPUT'), width: 70 },
				{ text: chalk.cyan('SIZE'), width: 15 },
				{ text: chalk.cyan('GZIP'), width: 15 }
			);
			let ui = cliui();
			ui.div(
				{ text: chalk.green('service.worker.js'), width: 70 },
				{ text: chalk.green(bundler.getFileSize()), width: 15 },
				{ text: chalk.green(bundler.getGzipSize()), width: 15 }
			);
			console.log(chalk.cyan(`WORKERC`), chalk.cyan(`|`), header.toString());
			console.log('       ', chalk.green(`|`), ui.toString());
		})
		.hook("before-single", () => { })
		.hook("after-single", () => { }).hook()
		.hook("output-file", () => { })
		.hook('before-output-pack', () => {
			let ui = cliui();
			ui.div(
				{ text: chalk.cyan('OUTPUT'), width: 70 },
				{ text: chalk.cyan('SIZE'), width: 15 },
				{ text: chalk.cyan('GZIP'), width: 15 }
			);
			console.log(chalk.cyan(`PACKAGE`), chalk.cyan(`|`), ui.toString());
		})
		.hook("output-pack", ({ data }) => {
			let { pack } = data;
			let ui = cliui();
			ui.div(
				{ text: chalk.green(pack.packName), width: 70 },
				{ text: chalk.green(pack.getFileSize()), width: 15 },
				{ text: chalk.green(pack.getGzipSize()), width: 15 }
			);
			console.log('       ', chalk.green(`|`), ui.toString());
		})
		.hook("after-output", ({ env }) => {
			let { sourceMap } = env;
			sourceMap.outputer.getLogInfo().forEach((info) => {
				// console.log(chalk(`MAKE ERROR [`).red, info.name, chalk(`]`).red);
				console.log(chalk.red(`  ERROR`), chalk.red(`|`), chalk.red(info.name));
				console.log(info.error);
			});
		})
		.hook("outputIndex", () => { })
		.hook("after-pack", () => { })
		.hook("file-edit", () => {
			// console.log(chalk(`UPDATED`).yellow, chalk(`|`).yellow, chalk(`${util.formatDate()}                    [EDIT]`).green);
		})
		.hook("file-add", () => {
			// console.log(chalk(`UPDATED`).yellow, chalk(`|`).yellow, chalk(`${util.formatDate()}                    [ADD]`).green);
		})
		.hook("file-remove", () => {
			// console.log(chalk(`UPDATED`).yellow, chalk(`|`).yellow, chalk(`${util.formatDate()}                    [REMOVE]`).green);
		})
		.hook("git-clone-start", ({ data }) => {
			let { url } = data;
			cloneSpinner && cloneSpinner.stop();
			cloneSpinner = ora({ color: "yellow", text: "CLONE " + url }).start();
		})
		.hook("git-clone-end", ({ data }) => {
			let { error } = data;
			cloneSpinner && cloneSpinner.stop();
			if (error) {
				console.log(error);
				console.log(chalk.red(error.message));
			}
		})
		.hook("git-auth-undefined", () => {
			let settingPath = Path.resolve(os.homedir(), './.ada-cloud-temp/setting.json');
			console.log(chalk.yellow(`no found setting file at ${settingPath}`));
		});
};